package com.wangyf.thread.c_020_locksupport;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * 测试LockSupport
 * Created by feng on 2021/1/14.
 */
public class LockSupportTest {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.println(i);
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (i == 5) {
                    LockSupport.park();
                }
                if (i == 6) {
                    LockSupport.park();
                }
            }
        });
        t.start();
        // 可以不加睡5秒 因为unpark可以在park之前进行调用，只是里面维护了一个count而已，
        // 所以支持多次 park unpark，但是需要在 使用park的线程 start之后
        TimeUnit.SECONDS.sleep(5);
        System.out.println("after 5 seconds");
        TimeUnit.SECONDS.sleep(1);
        System.out.println("after 6 seconds");
        LockSupport.unpark(t);
    }
}
